#ifndef Rpcserver_h__
#define Rpcserver_h__

#include <map>
#include "google/protobuf/service.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
#include "methoddescriptor.h"
#include "../mynet/TcpListener.h"
#include "../session.h"



namespace myrpc
{
	class RpcChannel
	{
	public:
		RpcChannel() = default;
		~RpcChannel() = default;
		bool RegisterService(::google::protobuf::Service *service);
		bool Start(std::string , int port);
		bool DoRpc(mynet::PackagePtr pack);
	
	protected:
	
		void addMethod(const MethodDes &method, const std::string &methodName)
		{
			_methods[methodName] = method;
		}
	
		const MethodDes* getMethod(const std::string &methodName) const
		{
			return &_methods.at(methodName);
		}
	
		bool isExistMethod(const std::string &methodName) const
		{
			return (_methods.find(methodName) != _methods.end());
		}

		void InitSession(const std::shared_ptr<myrpc::mynet::Session>& session);
	
	private:
		::google::protobuf::Service       *_service;
		std::map<std::string, MethodDes>  _methods;
		myrpc::mynet::TcpListener server;
		std::vector<std::shared_ptr<myrpc::mynet::Session>> _sessions;
	};
}



#endif // Rpcserver_h__

